package com.cyf.app.service.impl;

import com.cyf.app.network.bean.AppBusinessRsp;
import com.cyf.app.network.bean.parkinglock.*;
import com.cyf.app.service.AppParkingLockService;
import com.cyf.service.CheckParam;
import com.cyf.db.bean.User;
import com.cyf.db.bean.ParkingLockInfo;
import com.cyf.db.mapper.ParkingLockInfoMapper;
import com.cyf.service.bean.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 车位相关接口
 * Created by cage on 2017/4/26.
 */
@Service
public class AppParkingLockServiceImpl implements AppParkingLockService {

    private static final Logger logger = LoggerFactory.getLogger(AppParkingLockServiceImpl.class);

    @Autowired
    private ParkingLockInfoMapper parkingLockInfoMapper;
    @Autowired
    private CheckParam checkParam;

    @Override
    public AppAddLockRsp addLock(AppAddLockReq req) {
        AppAddLockRsp rsp = new AppAddLockRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            // 检查地锁名称是否重复、mac地址是否重复
            checkParam.checkLockExist(user.getId(), req.getName(), req.getMacAddress(), 0);
            // 保存
            ParkingLockInfo lockInfo = new ParkingLockInfo();
            lockInfo.setName(req.getName());
            lockInfo.setMacAddress(req.getMacAddress());
            lockInfo.setLockStatus(req.getLockStatus());
            lockInfo.setAppUserId(user.getId());
            lockInfo.setMobile(user.getMobile());
            lockInfo.setCreatedTime(new Date());
            parkingLockInfoMapper.insertLock(lockInfo);
            rsp.setResult(AppBusinessRsp.SUCCESS);
        } catch (CustomException e) {
            rsp.setStrError(e.getMessage());
        } catch (Exception e) {
            logger.error("添加车位异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }

    @Override
    public AppGetLocksRsp getParkingLocks(AppGetLocksReq req) {
        AppGetLocksRsp rsp = new AppGetLocksRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            List<ParkingLockInfo> lockInfos = parkingLockInfoMapper.getByAppUserId(user.getId());
            ArrayList<AppGetLocksItem> locks = new ArrayList<>();
            if (lockInfos != null && lockInfos.size() > 0) {
                for (ParkingLockInfo lockInfo : lockInfos) {
                    AppGetLocksItem item = new AppGetLocksItem();
                    item.setParkingLockId(lockInfo.getId());
                    item.setParkingLockName(lockInfo.getName());
                    item.setMacAddress(lockInfo.getMacAddress());
                    item.setLockStatus(lockInfo.getLockStatus());
                    item.setShareStatus(lockInfo.getShareStatus());
                    locks.add(item);
                }
            }
            rsp.setLocks(locks);
            rsp.setResult(AppBusinessRsp.SUCCESS);
        } catch (Exception e) {
            logger.error("获取车位异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }

    @Override
    public AppEditLockRsp editParkingLock(AppEditLockReq req) {
        AppEditLockRsp rsp = new AppEditLockRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            // 检查app用户是否有该地锁
            ParkingLockInfo lockInfo = checkParam.getLock(user.getId(), req.getParkingLockId());
            // 检查地锁名称是否重复、mac地址是否重复
            checkParam.checkLockExist(user.getId(), req.getName(), req.getMacAddress(), lockInfo.getId());
            // 更新
            lockInfo.setName(req.getName());
            lockInfo.setMacAddress(req.getMacAddress());
            parkingLockInfoMapper.updateLock(lockInfo);
            rsp.setResult(AppBusinessRsp.SUCCESS);
        } catch (CustomException e) {
            rsp.setStrError(e.getMessage());
        } catch (Exception e) {
            logger.error("修改车位信息异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }

    @Override
    public AppDeleteLockRsp deleteParkingLock(AppDeleteLockReq req) {
        AppDeleteLockRsp rsp = new AppDeleteLockRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            checkParam.getLock(user.getId(), req.getParkingLockId());
            parkingLockInfoMapper.deleteById(req.getParkingLockId());
            rsp.setResult(AppBusinessRsp.SUCCESS);
        } catch (CustomException e) {
            rsp.setStrError(e.getMessage());
        } catch (Exception e) {
            logger.error("删除车位异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }

    @Override
    public AppShareLockRsp shareParkingLock(AppShareLockReq req) {
        AppShareLockRsp rsp = new AppShareLockRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            if ("".equals(req.getToken()) || (req.getShareStatus() != 1 && req.getShareStatus() != 0)) {
                rsp.setStrError("参数错误！");
                return rsp;
            }
            ParkingLockInfo lockInfo = checkParam.getLock(user.getId(), req.getParkingLockId());
            if (req.getShareStatus() == lockInfo.getShareStatus()) {
                rsp.setStrError("请勿重复操作！");
                return rsp;
            }
            parkingLockInfoMapper.updateShareStatus(req.getShareStatus(), lockInfo.getId());
            rsp.setResult(AppBusinessRsp.SUCCESS);
        } catch (CustomException e) {
            rsp.setStrError(e.getMessage());
        } catch (Exception e) {
            logger.error("共享车位异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }

    @Override
    public AppLockNoticeRsp lockNotice(AppLockNoticeReq req) {
        AppLockNoticeRsp rsp = new AppLockNoticeRsp();
        rsp.setResult(AppBusinessRsp.FAIL);
        User user;
        try {
            user = checkParam.getAppUser(req.getToken());
        } catch (CustomException e) {
            rsp.setResult(AppBusinessRsp.TIMEOUT);
            rsp.setStrError(e.getMessage());
            return rsp;
        }
        try {
            if (req.getLockStatus() != 1 && req.getLockStatus() != 0) {
                rsp.setStrError("参数错误！");
                return rsp;
            }
//            AppUser appUser = checkParam.getAppUser(req.getToken());
//            ParkingLockInfo lockInfo = checkParam.getLock(appUser.getId(), req.getParkingLockId());
            ParkingLockInfo lockInfo = parkingLockInfoMapper.getById(req.getParkingLockId());
            if (lockInfo == null) {
                rsp.setStrError("没有此地锁！");
                return rsp;
            }
            parkingLockInfoMapper.updateLockStatus(lockInfo.getId(), req.getLockStatus());
            rsp.setResult(AppBusinessRsp.SUCCESS);
        }/* catch (CustomException e) {
            rsp.setStrError(e.getMessage());
        } */catch (Exception e) {
            logger.error("共享车位异常：{}", e);
            rsp.setStrError("服务器异常");
        }
        return rsp;
    }
}
